#include "thread_pool.h"
#include "tlv_protocol.h"
void*handler(void*arg){
    thread_pool_t *pthread_pool=(thread_pool_t*)arg;
    while(1){
        pthread_mutex_lock(&pthread_pool->mutex);
        while(pthread_pool->task_queue.size==0){
            pthread_cond_wait(&pthread_pool->cond,&pthread_pool->mutex);
        }
        int netfd=pthread_pool->task_queue.front->netfd;
        de_queue(&pthread_pool->task_queue);
        pthread_mutex_unlock(&pthread_pool->mutex);
        /* char*username=NULL; */
        /* if(gate(netfd,&username)==0){ */
            // 认证失败，连接已关闭，继续处理下一个任务
            /* continue; */
        /* } */
        /* char user_path[4096]; */
        /* snprintf(user_path, sizeof(user_path), "/%s", username); */
        path_stack_t path_stack;
        init_stack(&path_stack);
        char arr[100]={0};
        //char path[4096] = "/";
        while(1){
            bzero(arr,sizeof(arr));
            int command=handle_command(netfd,arr);
            if(command==CD){
                //切换目录
                printf("切换目录cd\n");
                int res=handle_cd(&path_stack,CPP68_PATH , arr);
                train_t train;
                if(res==0){
                    train.type=100;
                    train.length=0;
                    send(netfd,&train.type,4,MSG_NOSIGNAL);
                    send(netfd,&train.length,4,MSG_NOSIGNAL);
                }else{
                    train.type=101;
                    train.length=0;
                    send(netfd,&train.type,sizeof(train.type),MSG_NOSIGNAL);
                    send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL);

                }
            }
            else if(command==PWD){
                //展示当前工作目录
                printf("展示当前工作目录\n");
                handle_pwd(netfd, &path_stack);

            }
            else if(command==LS){
                //展示当前目录
                printf("展示当前目录下所有文件\n");
                /* handle_ls(netfd,&path_stack, CPP68_PATH); */

            }
            else if(command==GETS){
                //下载
                printf("下载\n");
                char real_path[256]={0};
                build_real_path(&path_stack,CPP68_PATH,real_path,sizeof(real_path));
                strcat(real_path, "/");
                strcat(real_path,arr);
                printf("real_path=%s\n",real_path);
                send_file_content(netfd, real_path);
            }
            else if(command==PUTS){
                //上传
                printf("上传\n");
                char real_path[256]={0};
                build_real_path(&path_stack,CPP68_PATH,real_path,sizeof(real_path));

                char filename_to_create[512] = { 0 };
                snprintf(filename_to_create, sizeof(filename_to_create), "%s/%s", real_path, arr);
                printf("filename_to_create=%s\n",filename_to_create);

                receive_and_write_file(netfd, filename_to_create);
            }
            else if(command==REMOVE){
                //删除文件
                printf("删除文件\n");
            }
            else if(command==MKDIR){
                //创建目录
                printf("创建目录\n");
            }
            else if(command==RMDIR){
                //删除目录
                printf("删除目录\n");
            }
            else if(command==QUIT){
                //退出
                printf("用户退出！\n");
                free_stack(&path_stack);
                close(netfd);
                break;
            }
            else{
                printf("无此命令!\n");
            }
            printf("command=%s\n",arr);

        }
    }
}
int make_worker(thread_pool_t*pthread_pool){
    for(int i=0;i<pthread_pool->tid_arr.worker_num;i++){
        pthread_create(&pthread_pool->tid_arr.arr[i],NULL,handler,pthread_pool);
    }
    return 0;
}
